package org.hagopa.games.moneypool.game;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.hagopa.games.moneypool.Configuration;
import org.hagopa.games.moneypool.dblayer.GameGeneralInfoAdopt;
import org.hagopa.games.moneypool.dblayer.Connector.DBConnector;

import org.hagopa.games.moneypool.network.NetClientHandler;
import org.hagopa.games.moneypool.network.Reception;

public class GamesManager implements Runnable{
	private static long totalId = 1;
	private HashMap <Long, GameThread> GamesPool = new HashMap<Long, GameThread>();
	private List<Long> newGameReqQ = null;
	private Reception waitingConnections = null;
	
		
	public GamesManager(Configuration cfg)
	{
	
		System.out.println("DB Connect data");
		System.out.println("Host: "+ Configuration.getDBHost());
		System.out.println("User:" + Configuration.getUser());
		DBConnector nDB = DBConnector.getInstance();
		DBConnector.setInitialSettings(Configuration.getDBHost(),Configuration.getUser(), 
									Configuration.getPwd(), Configuration.getDbname());
		if(null == nDB)
		{
			System.out.println("Failed to create DB handler!!");
			return;
		}
		DBConnector.connect();
	}
	
	public void setRequestsQ(List<Long> outQ)
	{
		newGameReqQ = outQ;
	}
	
	public void linkWaitingConnections(Reception rcp)
	{
		this.waitingConnections = rcp;
		if(this.waitingConnections == null)
		{
			System.out.println("Failed to link Reception");
		}
		System.out.println("Num of Waiting cons in Reception : " + this.waitingConnections.getWaitingClientsNo());
		
	}
	
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("Games Manager Thread Started!!!");
		while(true)
		{
			synchronized (this.newGameReqQ) 
			{
				
				Iterator<Long> itr = this.newGameReqQ.iterator();
				while(itr.hasNext())
				{
					Long waitClientId = itr.next();
					System.out.println("\n-------------------------------------\n");
					System.out.println("Cliend from Q: " + waitClientId.toString());
					
				
					System.out.println("\nWaitingConnections size: " + this.waitingConnections.getWaitingClientsNo());
					Iterator<NetClientHandler> clItr = waitingConnections.getIter();
					System.out.println("[GamesManager]Looking for waiting connections");
					while(clItr.hasNext())
					{
						System.out.println("\nWaiting Client Connection No: " +this.waitingConnections.getWaitingClientsNo());
						NetClientHandler tmpClient = clItr.next();
						
						if(waitClientId.equals(new Long(tmpClient.getClientId())))
						{
							
							//Pass players into the game - TODO: implement
					//		PlayerData plData = new PlayerData(tmpClient.getClientId());
//							plData.loadData();
//							PlayerHolder tmpPlayer = new PlayerHolder(plData);
//							System.out.println("new player Id: " + tmpPlayer.getGId());
//							tmpPlayer.setType(1);
//							tmpPlayer.setClientHandler(tmpClient, true);
//							
//							
							GameThread tmpGame = CreateNewGame(tmpClient.getGameId());
							//tmpGame.appendPlayerToGame(tmpPlayer);

						}
					}
					
					
					itr.remove();
				}
			}
			
		}
		
	}
	
	private GameThread CreateNewGame(long gameId)
	{
		
		
		if( this.GamesPool.containsKey(gameId) == true)
		{
			return this.GamesPool.get(gameId);
		}
		else
		{
			GameGeneralInfoAdopt gdt = new GameGeneralInfoAdopt(gameId);
			gdt.requestDB();
			GamesManager.totalId++;
			GameThread tgm = new GameThread(GamesManager.totalId, gdt);
			this.GamesPool.put(new Long(gdt.getGame_id()), tgm);
			Thread GThread = new Thread(tgm);
			GThread.start();
			return tgm;
		}
	
	}
	
	
	public void stopAll()
	{
		DBConnector.disconnect();
	}

}